package com.logistic.server.controller;

import com.logistic.server.common.annotation.LoginRequired;
import com.logistic.server.common.utils.SecurityUtils;
import com.logistic.server.entity.common.PageRequest;
import com.logistic.server.entity.common.Result;
import com.logistic.server.entity.common.ResultGenerator;
import com.logistic.server.entity.pojo.SysConfig;
import com.logistic.server.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 参数配置 前端控制器
 * </p>
 *
 * @author swen
 * @since 2021-10-25
 */
@Api(value = "参数配置接口", tags = {"参数配置接口"})
@Slf4j
@LoginRequired
@RestController
@RequestMapping("/system/config")
public class SysConfigController {

     @Resource
     private ISysConfigService configService;

     /**
      * 参数配置列表
      * @param config 参数配置
      * @param pageRequest 分页参数
      * @return Result
      */
     @ApiOperation(value = "参数配置列表", notes = "参数配置列表")
     @GetMapping
     public Result<?> list(SysConfig config, PageRequest pageRequest) {
         return ResultGenerator.genSuccessResult(configService.selectPageConfigList(config, pageRequest));
     }

     /**
      * 参数配置新增
      * @param config 参数配置
      * @return Result
      */
     @ApiOperation(value = "参数配置新增", notes = "参数配置新增")
     @PostMapping
     public Result<?> add(@RequestBody SysConfig config) {
         configService.insertConfig(config);
         return ResultGenerator.genSuccessResult();
     }

     /**
      * 参数配置删除
      * @param ids 参数配置主键
      * @return Result
      */
     @ApiOperation(value = "参数配置删除", notes = "参数配置删除")
     @DeleteMapping("/{ids}")
     public Result<?> delete(@PathVariable Long[] ids) {
         configService.deleteConfigByIds(ids);
         return ResultGenerator.genSuccessResult();
     }

     /**
      * 参数配置修改
      * @param config 参数配置
      * @return Result
      */
     @ApiOperation(value = "参数配置修改", notes = "参数配置修改")
     @PutMapping
     public Result<?> update(@RequestBody SysConfig config) {
         config.setUpdateBy(SecurityUtils.getUsername());
         config.setUpdateTime(new Date());
         return ResultGenerator.genSuccessResult(configService.updateById(config));
     }

     /**
      * 参数配置详情
      * @param id 参数配置主键
      * @return Result
      */
     @ApiOperation(value = "参数配置详情", notes = "参数配置详情")
     @GetMapping("/{id}")
     public Result<?> detail(@PathVariable Long id) {
         return ResultGenerator.genSuccessResult(configService.selectConfigById(id));
     }

    /**
     * 根据参数键名查询参数值
     * @param configKey key
     * @return Result
     */
    @GetMapping(value = "/configKey/{configKey}")
    public Result<?> getConfigKey(@PathVariable String configKey) {
        return ResultGenerator.genSuccessResult(configService.selectConfigByKey(configKey));
    }

    /**
     * 刷新参数缓存
     */
    @DeleteMapping("/refreshCache")
    public Result<?> refreshCache() {
        configService.resetConfigCache();
        return ResultGenerator.genSuccessResult();
    }
}
